package cn.iocoder.yudao.module.member.controller.admin.usersharelogs;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.usersharelogs.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usersharelogs.UserShareLogsDO;
import cn.iocoder.yudao.module.member.service.usersharelogs.UserShareLogsService;

@Tag(name = "管理后台 - 用户推广日志")
@RestController
@RequestMapping("/member/user-share-logs")
@Validated
public class UserShareLogsController {

    @Resource
    private UserShareLogsService userShareLogsService;

    @PostMapping("/create")
    @Operation(summary = "创建用户推广日志")
    @PreAuthorize("@ss.hasPermission('member:user-share-logs:create')")
    public CommonResult<Long> createUserShareLogs(@Valid @RequestBody UserShareLogsSaveReqVO createReqVO) {
        return success(userShareLogsService.createUserShareLogs(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户推广日志")
    @PreAuthorize("@ss.hasPermission('member:user-share-logs:update')")
    public CommonResult<Boolean> updateUserShareLogs(@Valid @RequestBody UserShareLogsSaveReqVO updateReqVO) {
        userShareLogsService.updateUserShareLogs(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户推广日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-share-logs:delete')")
    public CommonResult<Boolean> deleteUserShareLogs(@RequestParam("id") Long id) {
        userShareLogsService.deleteUserShareLogs(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户推广日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-share-logs:query')")
    public CommonResult<UserShareLogsRespVO> getUserShareLogs(@RequestParam("id") Long id) {
        UserShareLogsDO userShareLogs = userShareLogsService.getUserShareLogs(id);
        return success(BeanUtils.toBean(userShareLogs, UserShareLogsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户推广日志分页")
    @PreAuthorize("@ss.hasPermission('member:user-share-logs:query')")
    public CommonResult<PageResult<UserShareLogsRespVO>> getUserShareLogsPage(@Valid UserShareLogsPageReqVO pageReqVO) {
        PageResult<UserShareLogsDO> pageResult = userShareLogsService.getUserShareLogsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserShareLogsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户推广日志 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-share-logs:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserShareLogsExcel(@Valid UserShareLogsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserShareLogsDO> list = userShareLogsService.getUserShareLogsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户推广日志.xls", "数据", UserShareLogsRespVO.class,
                        BeanUtils.toBean(list, UserShareLogsRespVO.class));
    }

}